//商品列表
var app = getApp(); //全局APP

Page({
	//页面的初始数据
	data: {
		nickname: '',
		avatar: '../../image/default_avatar.png',
		listData: [],
		selectNum: 0,
		checkboxAll: false,
		buttonDisabled: false // 按钮是否可用
	},

	//加载执行
	onShow: function() {
		var page = this;

		// 获取用户信息
		app.api.userInfo(function(res) {
			if (res.status) {
				//如果没有头像，设置本地默认头像
				var avatar = '../../image/default_avatar.png';
				if (res.data.avatar) {
					avatar = res.data.avatar;
				}
				page.setData({
					nickname: res.data.nickname,
					avatar: avatar
				});
			}
		});
 
		page.getChangeMajorStudentList({});
	},
	
	getChangeMajorStudentList: function(data) {
		var page = this;
		// 获取待提交班级列表
		app.api.getChangeMajorStudentList(data, function(res) {
			if (res.status) {
				page.setData({
					listData: res.data
				});
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					listData: []
				});
			}
			page.isAllCheckbox();
		});
	},

	// 选择事件处理
	itemSelected: function(e) {
		var page = this;
		var bjdm = e.currentTarget.dataset.bjdm;

		var listData = page.data.listData;
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].bjdm == bjdm) {
				if (listData[i].status == true) {
					listData[i].status = false;
				} else {
					listData[i].status = true;
				}
			}
		}

		page.setData({
			listData: listData
		});

		page.isAllCheckbox();
	},

	//是否全选
	isAllCheckbox: function() {
		var page = this;
		var listData = page.data.listData;
		var flag = true;
		var num = 0;

		if (!listData && typeof(listData) != "undefined") {
			flag = false;
			num = 0;
		} else {
			for (var i = 0; i < listData.length; i++) {
				if (listData[i].status == false) {
					flag = false;
				} else {
					num += parseInt(listData[i].rs);
				}
			}

			if (listData.length <= 0) {
				flag = false;
				num = 0;
			}
		}

		this.setData({
			checkboxAll: flag,
			selectNum: num
		});
	},

	//全选操作
	checkboxAll: function(e) {
		if (this.data.checkboxAll == true) {
			this.setData({
				checkboxAll: false
			});
			this.setAllCheckbox(false);
		} else {
			this.setData({
				checkboxAll: true
			});
			this.setAllCheckbox(true);
		}
	},

	//全选设置
	setAllCheckbox: function(e) {
		var page = this;
		var listData = page.data.listData;
		if (e) {
			//全选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = true;
			}
		} else {
			//全不选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = false;
			}
		}
		page.setData({
			listData: listData
		});

		page.isAllCheckbox();
	},
 
	showDetail: function(e) {
		var page = this;
		var bjdm = e.currentTarget.dataset.bjdm;

		wx.navigateTo({
			url: '../ChangeMajorStudentDetail/ChangeMajorStudentDetail?bjdm=' + bjdm
		});
	},

	submit: function(e) {
		var page = this; 
		if(page.data.selectNum == 0) {
			app.common.errorToBack("没有需要提交的学生！", 0);
			return;
		}
		
		var listData = page.data.listData;
		
		var newData = '';
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].status == true) {
				newData += ',' + listData[i].bjdm;
			} 
		}
		
		app.common.buttonDisabled(this);
		
		if (newData.substr(0, 1) == ',') {
			newData = newData.substr(1);
		}
		
		var data = {
			bjdm_ids: newData
		};
		
		// 提交申请
		app.api.changeMajorStudentConfirmList(data, function(res) {
			app.common.successToShow(res.msg, function() {
				page.getChangeMajorStudentList({});
			});
		});
	},
});
